/*****************************************
 *  所有版权(c) 上海塔齐通讯技术有限公司       *
 *****************************************/
package com.touch.sysif.sms.api.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.UUID;

import com.touch.sysif.sms.api.SmsClient;

/**
 * 
 * @version $Revision: 1.0 $ 
 * @author yanxiao
 * @date: Jan 17, 2014
 * @time: 10:22:01 PM
 */
public abstract class AbstractSmsClient implements SmsClient {
	
	protected Properties config = new Properties();
	protected boolean isRun;
	protected ActiveTestThread testThread;
	protected String username;
	protected String password;
	
	
	/**
	 * @see SmsClient#init(String)
	 */
	public void init(String configFilePath) {
		try {
			File configFile = new File(configFilePath);
//			System.out.println("配置文件路径："+configFile.getAbsolutePath());
//			config.load(new FileInputStream(configFile));
			InputStream is=this.getClass().getClassLoader().getResourceAsStream(configFilePath);
			config.load(is);
			username = config.getProperty("username");
			password = config.getProperty("password");
			testThread = new ActiveTestThread();
			isRun = true;
			testThread.start();
		} catch (Exception e) {
			throw new RuntimeException("初始化失败,请检查配置文件！");
		}
	}
	
	class ActiveTestThread extends Thread {
		@Override
		public void run() {
			while (isRun && !this.isInterrupted()) {
				try {
					Thread.sleep(60000);
					activeTest();
				} catch (Exception e) {
				}
			}
		}
	}
	/**
	 * 生成一个32位的UUID
	 * @return
	 */
	public static String generateUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	public void shutdown() {
		isRun = false;
		testThread.interrupt();
	}
}
